<?php
function dpa_detailview( $user_id, $achievement ) {
	$achievement_date = dpa_get_unlocked( 'achieved_at', array( array( 'type' => 'd', 'key' => 'user_id', 'value' => $user_id ), array( 'type' => 's', 'key' => 'short_name', 'value' => $achievement['short_name'] ) ), 1 );

	if ( !$achievement_date ) {
		$icon = 'cross.png';
		$alt = __( 'Achievement has not been unlocked', 'dp-achievements' );
		$extra = '';
	} else {
		$icon = 'tick.png';
		$alt = __( 'Achievement has been unlocked', 'dp-achievements' );
		$extra = sprintf( ' <span class="dpa-title">%s</span>', apply_filters( 'dpa_filter_achievement_date', mysql2date( get_option( 'date_format' ), $achievement_date ) ) );
	}
?>
	<li>
		<img src="<?php echo WP_PLUGIN_URL . '/achievements/dp-achievements/images/' . $icon; ?>" alt="<?php echo $alt; ?>" />
		<p><span class="dpa-title"><?php echo apply_filters( 'dpa_filter_achievement_nice_name', __( $achievement['nice_name'], 'dp-achievements' ) ); ?></span> - <?php echo apply_filters( 'dpa_filter_achievement_description', __( $achievement['description'], 'dp-achievements' ) ); echo $extra; ?></p>
	</li>
<?php
}

function dpa_draw_categorycompletiondetails( $user_id ) {
	//number of ACTIVE achievements PER CATEGORY
	$categories = dpa_get( 'DISTINCT category', array( array( 'type' => 'd', 'key' => 'active', 'value' => 1 ) ) );
	$total_achievements = array();
	$earnt_achievements = array();

	foreach ( (array) $categories as $category ) {
		$total_achievements[$category] = dpa_get( 'COUNT(short_name)', array( array( 'type' => 's', 'key' => 'category', 'value' => $category ), array( 'type' => 'd', 'key' => 'active', 'value' => 1 ) ) );
	}

	//number of EARNT achievements PER CATEGORY
	foreach ( (array) $categories as $category ) {
		$earnt_achievements[$category] = dpa_get_unlocked( 'COUNT(short_name)', array( array( 'type' => 'd', 'key' => 'user_id', 'value' => $user_id ), array( 'type' => 's', 'key' => 'short_name', 'value' => '' ), array( 'type' => 's', 'key' => 'category', 'value' => $category ), array( 'type' => 'd', 'key' => 'active', 'value' => 1 ) ) );
	}

	$stats = array();
	foreach ( (array) $categories as $category ) {
		$stats[$category] = array( 'name' => $category,
															 'total' => $total_achievements[$category],
															 'earnt' => $earnt_achievements[$category],
															 'percentage' => ( $earnt_achievements[$category] / $total_achievements[$category] ) * 100 );
	}
	
	foreach ( (array) $stats as $s ) {

		$class = 'bar';
		if ( $s['percentage'] < 100 )
			$class .= ' flatright';
	?>
	<div class="graph halfsize">
		<div class="<?php echo $class; ?>" style="width: <?php echo $s['percentage']; ?>%"></div>
		<div class="caption"><p><?php echo apply_filters( 'dpa_filter_achievement_nice_name', __( $s['name'], 'dp-achievements' ) ); ?>: <?php echo $s['earnt']; ?> / <?php echo $s['total']; ?></p></div>
	</div>
	<?php
	}
}

function dpa_draw_overallcompletiondetails( $user_id ) {
	$earnt  = (int) dpa_get_count_of_ea( $user_id );
	$active = (int) dpa_get( 'COUNT(id)', array( array( 'type' => 'd', 'key' => 'active', 'value' => 1 ) ) );
	$percentage = ($earnt / $active) * 100;

	$class = 'bar';
	if ( $s['percentage'] < 100 )
		$class .= ' flatright';
	?>
	<div class="graph fullsize">
		<div class="<?php echo $class; ?>" style="width: <?php echo $percentage; ?>%"></div>
		<div class="caption"><p>Total Completed: <?php echo $earnt; ?> / <?php echo $active; ?></p></div>
	</div>
	<?php
}

function dpa_get_last_five_unlocked_achievements( $user_id ) {
	return dpa_get_unlocked( 'nice_name,description,achieved_at', array( array( 'type' => 'd', 'key' => 'user_id', 'value' => $user_id ), array( 'type' => 's', 'key' => 'short_name', 'value' => '' ), array( 'type' => 'd', 'key' => 'active', 'value' => 1 ) ), 5, 'achieved_at DESC' );
}
?>
